using System.Collections.ObjectModel;
using System.Xml.Serialization;
using Chaotic.DataBase.DBAccessLibrary.Common;

namespace Chaotic.DataBase.DBAccessLibrary.Model;

[Serializable]
[XmlInclude(typeof(DBEntity))]
[XmlInclude(typeof(ViewEntity))]
[XmlInclude(typeof(ServerEntity))]
[XmlInclude(typeof(DescriptionEntity))]
[XmlInclude(typeof(FieldEntity))]
[XmlInclude(typeof(TableEntity))]
[XmlInclude(typeof(ProcEntity))]
public class DataBaseEntity(string name) : BasePropertyChange
{
	private string _name = name;

	private string? _chName = string.Empty;

	private string _imageUrl= "/Images/Tree/WordWrap.png";

    private bool _isExpanded;

	private bool _isSelected;

	private ObservableCollection<DataBaseEntity> _childNodes = new();

    public string Name
	{
		get => _name;
        set
		{
			if (value != _name)
			{
				_name = value;
				this.NotifyPropertyChanged((o) => o.Name);
			}
		}
	}

	public string? ChName
	{
		get => _chName;
        set
		{
			if (value != _chName)
			{
				_chName = value;
				this.NotifyPropertyChanged((o) => o.ChName);
			}
		}
	}

	public string ImageUrl
	{
		get => _imageUrl;
        set
		{
			if (value != _imageUrl)
			{
				_imageUrl = value;
				this.NotifyPropertyChanged((o) => o.ImageUrl);
			}
		}
	}

	public bool IsExpanded
	{
		get => _isExpanded;
        set
		{
			if (value != _isExpanded)
			{
				_isExpanded = value;
				this.NotifyPropertyChanged((o) => o.IsExpanded);
			}
		}
	}

	public bool IsSelected
	{
		get => _isSelected;
        set
		{
			if (value != _isSelected)
			{
				_isSelected = value;
				this.NotifyPropertyChanged((o) => o.IsSelected);
			}
		}
	}

	public ObservableCollection<DataBaseEntity> ChildNodes
	{
		get => _childNodes;
        set
		{
			if (value != _childNodes)
			{
				_childNodes = value;
				this.NotifyPropertyChanged((o) => o.ChildNodes);
			}
		}
	}

	public int Count => ChildNodes.Count;

    public bool IsTrans { get; set; }

	public string? TransChName
	{
		get
		{
			if (IsTrans)
			{
				return Name;
			}
			return ChName;
		}
		set
		{
			if (IsTrans)
			{
				Name = value;
			}
			else
			{
				ChName = value;
			}
		}
	}

	public string? TransName
	{
		get
		{
			if (IsTrans)
			{
				return ChName;
			}
			return Name;
		}
		set
		{
			if (IsTrans)
			{
				ChName = value;
			}
			else
			{
				Name = value;
			}
		}
	}

    public void TransForm()
	{
		foreach (DataBaseEntity childNode in ChildNodes)
		{
			(childNode.ChName, childNode.Name) = (childNode.Name, childNode.ChName);
			childNode.IsTrans = !childNode.IsTrans;
		}
	}

	public virtual void ClearState()
	{
		IsExpanded = false;
		IsSelected = false;

		DataBaseEntity[] array = ChildNodes.ToArray();
		foreach (DataBaseEntity dataBaseEntity in array)
		{
			dataBaseEntity.ClearState();
		}
	}

	public virtual DataBaseEntity Copy()
	{
		DataBaseEntity dataBaseEntity = new DataBaseEntity(Name);
		Copy(dataBaseEntity);

        return dataBaseEntity;
	}

	protected void Copy(DataBaseEntity entity)
	{
		entity.IsTrans = IsTrans;
		entity.ImageUrl = ImageUrl;
		entity.ChName = ChName;
	}
}
